package com.the4man.framework.service.impl;

import com.the4man.framework.dao.RoleMapper;
import com.the4man.framework.dao.RoleMoudleMapper;
import com.the4man.framework.dao.UserMapper;
import com.the4man.framework.model.Moudle;
import com.the4man.framework.model.Role;
import com.the4man.framework.model.User;
import com.the4man.framework.pagemodel.PUser;
import com.the4man.framework.service.UserInfoServiceI;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.EncryptUtil;
import com.the4man.framework.util.PageJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoServiceI {
    private static final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);
    @Autowired
    public UserMapper userMapper;
    @Autowired
    public RoleMapper roleMapper;
    @Autowired
    public RoleMoudleMapper roleMoudleMapper;

    public UserInfoServiceImpl() {
    }

    public User getUser(User user) {
        User userInfo = this.userMapper.query(user);
        if(userInfo != null && !"".equals(userInfo.getRoleid())) {
            Role role = this.roleMapper.selectByPrimaryKey(userInfo.getRoleid());
            if(role != null) {
                userInfo.setRole(role);
                List<Moudle> moudles = this.roleMoudleMapper.selectModelsByRoleid(userInfo.getRoleid());
                userInfo.getRole().setMoudles(moudles);
            }
        }

        return userInfo;
    }

    public void validateDqmUser(PageJson pageJson, PUser puser) {
        String s1 = DateUtil.getInstance().formatDate(new Date());
        String s2 = "2017-6-30 00:00:00";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        try {
            c1.setTime(df.parse(s1));
            c2.setTime(df.parse(s2));
        } catch (ParseException var12) {
            System.err.println("格式不正确");
        }

        c1.compareTo(c2);
        boolean flag = true;

        try {
            User user = new User();
            user.setUserid(puser.getUserid());
            user.setLoginType(puser.getLoginType());
            user = this.getUser(user);
            if(user != null) {
                if(!EncryptUtil.getInstance().encryptMD5(puser.getUserpwd()).equals(user.getUserpwd())) {
                    pageJson.setReturnMsg("密码错误");
                    flag = false;
                }
            } else {
                pageJson.setReturnMsg("用户不存在");
                flag = false;
            }

            pageJson.setReturnFlag(flag);
        } catch (Exception var11) {
            logger.error("用户登录出现异常", var11);
            flag = false;
            pageJson.setReturnFlag(flag);
            pageJson.setReturnMsg("系统内部错误");
        }

    }

    public User selectUserByUserId(String userId) {
        return this.userMapper.selectByPrimaryKey(userId);
    }
}
